package com.carrey.common.utilsrk;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
	/**
	 * 根据传入的日期获取年龄
	 */
	public static int getAge (Date src) {
		Calendar instance = Calendar.getInstance();
		Date time = instance.getTime();
		int year = time.getYear();
		instance.setTime(src);
		int year2 = src.getYear();
		int y = year-year2;
		return y;
	}
	/**
	 * 根据传入的参数获取该日期的月初日期，例如给定的日期为“2019-09-19 19:29:39”，返回“2019-09-01 00:00:00”
	 * @param src
	 * @return
	 */
	public static Date getDateByMonthInit (Date src) {
		Calendar instance = Calendar.getInstance();
		instance.setTime(src);
		instance.set(instance.YEAR, instance.MONTH, 1, 0, 0, 0);
		return instance.getTime();
	}
	
	/**
	 * 根据传入的参数获取该日器的月末日期，例如给定的日期为“2019-09-19 19:29:39”，返回“2019-09-30 23:59:59”，注意月大月小以及闰年
	 * @param src
	 * @return
	 */
	public static Date getDateByMonthLast(Date src) {
		Calendar instance = Calendar.getInstance();
		instance.setTime(src);
		instance.add(instance.MONTH, 1);
		instance.set(instance.YEAR, instance.MONTH, 1, 0, 0, 0);
		instance.add(instance.SECOND, -1);
		return instance.getTime();
	}
	/**
	 * 求未来日期离今天还剩的天数
	 * @param future
	 * @return
	 */
	public static int getDaysByFuture (Date future) {
		Calendar instance = Calendar.getInstance();
		Date time = instance.getTime();
		instance.setTime(future);
		long longtime = future.getTime()-time.getTime();
		int day = (int) (longtime);
		day = day / 60 /1000/ 60 / 24;
		return day;
	}
	/**
	 * 求过去日期离今天过去的天数
	 * @param departed
	 * @return
	 */
	public static int getDaysByDeparted (Date departed) {
		Calendar instance = Calendar.getInstance();
		Date time = instance.getTime();
		instance.setTime(departed);
		long longtime = time.getTime()-departed.getTime();
		int day = (int) (longtime);
		day = day / 60 /1000/  60 / 24;
		return day;
	}

}
